/**
 * Author : hoperun
 * Copyright (c) 2011 hoperun.
 * All rights reserved
 */

package com.hoperun.product.dao;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

/**
 * ContentProvider
 * 
 * @author mazhicheng
 * @version 1.0
 */
public class MIPDBProvider extends ContentProvider
{
    
    private static final UriMatcher MATHCER = new UriMatcher(UriMatcher.NO_MATCH);
    
    @SuppressWarnings("unused")
    private static final String     TAG     = "MIPProvider";
    
    private MIPDatabaseHelper       MIPProvider;
    
    static
    {
        
        MTypeInfoTable.getUriMatcherMailInfo(MATHCER);
    }
    
    @Override
    public boolean onCreate()
    {
        if (null == MIPProvider)
        {
            MIPProvider = new MIPDatabaseHelper(this.getContext());
            MIPProvider.getReadableDatabase();
        }
        return (null == MIPProvider) ? false : true;
    }
    
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs)
    {
        String tableName = getTableName(uri);
        if (null == tableName)
        {
            throw new IllegalArgumentException(Constant_DB.CONTENTPROVIDER_UNRECOGNIZED_URI + uri);
        }
        SQLiteDatabase db = MIPProvider.getWritableDatabase();
        int row = db.delete(tableName, selection, selectionArgs);
        if (row > 0)
        {
            getContext().getContentResolver().notifyChange(uri, null);
        }
        
        return row;
    }
    
    @Override
    public Uri insert(Uri uri, ContentValues values)
    {
        if ((null == values) || (0 == values.size()) || (null == uri))
        {
            return null;
        }
        
        String tableName = getTableName(uri);
        if (null == tableName)
        {
            throw new IllegalArgumentException(Constant_DB.CONTENTPROVIDER_UNRECOGNIZED_URI + uri);
        }
        
        SQLiteDatabase db = MIPProvider.getWritableDatabase();
        long row = db.insert(tableName, null, values);
        // long row = db.replace(tableName, "NULL", values);
        if (row > 0)
        {
            getContext().getContentResolver().notifyChange(uri, null);
            return ContentUris.withAppendedId(uri, row);
        }
        else
        {
            try
            {
                throw new SqlException();
                
            }
            catch (SqlException e)
            {
                e.printStackTrace();
            }
            return null;
        }
        
    }
    
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder)
    {
        
        String tableName = getTableName(uri);
        if (null == tableName)
        {
            throw new IllegalArgumentException(Constant_DB.CONTENTPROVIDER_UNRECOGNIZED_URI + uri);
        }
        
        SQLiteDatabase db = MIPProvider.getReadableDatabase();
        Cursor cursor = db.query(tableName, projection, selection, selectionArgs, null, null, sortOrder);
        
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
        
    }
    
    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs)
    {
        
        String tableName = getTableName(uri);
        if (null == tableName)
        {
            throw new IllegalArgumentException(Constant_DB.CONTENTPROVIDER_UNRECOGNIZED_URI + uri);
        }
        SQLiteDatabase db = MIPProvider.getWritableDatabase();
        int count = 0;
        count = db.update(tableName, values, selection, selectionArgs);
        
        getContext().getContentResolver().notifyChange(uri, null);
        return count;
    }
    
    /**
     * get database table name according to URI
     * 
     * @param uri URI
     * @return table name
     */
    private static String getTableName(Uri uri)
    {
        if (null == uri)
        {
            return null;
        }
        switch (MATHCER.match(uri))
        {
            case MTypeInfoTable.TYPEINFO_INFO:
                return MTypeInfoTable.TABLE_NAME;
            default:
                return null;
                
        }
    }
    
    @Override
    public String getType(Uri uri)
    {
        // int type = MATHCER.match(uri);
        
        return Constant_DB.CONTENT_TYPE;
    }
}
